export function getIndex(obj, id) {
    var i = 0,
        l = obj.length;
    while (i < l) {
        if (obj[i].id && obj[i].id == id) {
            break;
        }
        i++;
    }
    return i;
}
export function getItem(obj, id) {
    var index = getIndex(obj, id)
    return obj[index];
}

export function setItem(obj, id, newObj) {
    var index = getIndex(obj, id)
        //如果传入新Item则为替换，否则为删除
    if (newObj) {
        obj.splice(index, 1, newObj);
    } else {
        obj.splice(index, 1);
    }
    return obj;
}
export function addItem(obj, newObj) {
    obj.push(newObj);
}

// const obj = [{"opacity": 1.0, "sort": 0, "tip_text": "\u62e8\u6253\u7535\u8bdd", "repeat": 1, "icon_class": "icon-phone", "color": "0EA090", "focus": false, "tem_widget_change_dis": 0, "height": 45, "visible": true, "shape": "circle", "radius": 4.0, "auto_play": 1, "tem_widget_change": 1, "label": "\u6253\u7535\u8bdd1", "id": "58462c7d9cf82200054f0343", "icon": 1, "rotate_x": 0, "rotate_y": 0, "rotate_z": 0, "scale": 0.0, "single_page": 0, "readOnly": false, "custom": 0, "width": 135, "offset_x": -157, "offset_y": 349, "offset_z": 0, "action": {"type": 0, "number": "45782"}, "is_saved": true, "type": 0, "resize_lock": -1}, {"opacity": 1.0, "sort": 1, "tip_text": "\u62d3\u5c55\u4ecb\u7ecd", "repeat": 1, "icon_class": "icon-image-text", "color": "0EA090", "focus": false, "tem_widget_change_dis": 0, "height": 46, "visible": true, "shape": "circle", "radius": 4.0, "auto_play": 1, "tem_widget_change": 1, "label": "\u62d3\u5c55\u4ecb\u7ecd1", "id": "58462c7d9cf82200054f0344", "icon": 1, "rotate_x": 0, "rotate_y": 0, "rotate_z": 0, "scale": 0.0, "single_page": 0, "readOnly": false, "custom": 0, "width": 138, "offset_x": 154, "offset_y": 346, "offset_z": 0, "action": {"content": "<p>asdfa</p>", "type": 3}, "is_saved": true, "type": 3, "resize_lock": -1}, {"opacity": 1.0, "sort": 2, "tip_text": "WEB\u94fe\u63a5", "repeat": 1, "icon_class": "icon-link", "color": "0EA090", "focus": false, "tem_widget_change_dis": 0, "height": 45, "visible": true, "shape": "circle", "radius": 4.0, "auto_play": 1, "tem_widget_change": 1, "label": "WEB\u94fe\u63a51", "id": "58462c7d9cf82200054f0347", "icon": 1, "rotate_x": 0, "rotate_y": 0, "rotate_z": 0, "scale": 0.0, "single_page": 0, "readOnly": false, "custom": 0, "width": 135, "offset_x": -157, "offset_y": 282, "offset_z": 0, "action": {"url": "http://baidu.com", "type": 1}, "is_saved": true, "type": 1, "resize_lock": -1}, {"opacity": 1.0, "sort": 3, "tip_text": "", "repeat": 0, "icon_class": "icon-images", "color": "0EA090", "focus": false, "tem_widget_change_dis": 0, "height": 95, "visible": true, "readOnly": false, "radius": 0.0, "auto_play": 1, "tem_widget_change": 1, "label": "\u76f8\u518c2", "id": "58462c7d9cf82200054f0348", "icon": 1, "rotate_x": 0, "rotate_y": 0, "rotate_z": 0, "scale": 1.0, "single_page": 0, "url": "http://yxfile.idealsee.com/a79fd9cec650473f4d4852ecf76a1f9d_jpg.jpg?imageView2/0/w/480/h/0", "custom": 1, "width": 125, "html": "IMG_20140802_101335.jpg", "offset_x": 89, "offset_y": -87, "offset_z": 0, "action": {"type": -1, "carousel": [{"url": "http://yxfile.idealsee.com/a79fd9cec650473f4d4852ecf76a1f9d_jpg.jpg?imageView2/0/w/480/h/0", "width": 640, "html": "IMG_20140802_101335.jpg", "md5": "a79fd9cec650473f4d4852ecf76a1f9d_jpg", "height": 480}, {"url": "http://yxfile.idealsee.com/63944356a08d9f5adddc7914aaa99cb1_jpg.jpg?imageView2/0/w/480/h/0", "width": 640, "html": "IMG_20140802_113258.jpg", "md5": "63944356a08d9f5adddc7914aaa99cb1_jpg", "height": 480}, {"url": "http://yxfile.idealsee.com/f0ca67791c29eb9979a3946ce65dee3a_jpg.jpg?imageView2/0/w/480/h/0", "width": 640, "html": "IMG_20140802_113314.jpg", "md5": "f0ca67791c29eb9979a3946ce65dee3a_jpg", "height": 480}, {"url": "http://yxfile.idealsee.com/aad97ea288337cb2ef466d997b635ea0_jpg.jpg?imageView2/0/w/480/h/0", "width": 640, "html": "IMG_20140802_113320.jpg", "md5": "aad97ea288337cb2ef466d997b635ea0_jpg", "height": 480}, {"url": "http://yxfile.idealsee.com/de9a39507b9c7437e634aa76f53e2166_jpg.jpg?imageView2/0/w/480/h/0", "width": 640, "html": "IMG_20140802_113317.jpg", "md5": "de9a39507b9c7437e634aa76f53e2166_jpg", "height": 480}, {"url": "http://yxfile.idealsee.com/c9b5193c6f2c0363304fd509af1d8bb6_jpg.jpg?imageView2/0/w/480/h/0", "width": 640, "html": "IMG_20140802_113338.jpg", "md5": "c9b5193c6f2c0363304fd509af1d8bb6_jpg", "height": 480}, {"url": "http://yxfile.idealsee.com/988c2bd44be09e8c4fc1df5514e3e5de_jpg.jpg?imageView2/0/w/480/h/0", "width": 640, "html": "IMG_20140802_113519.jpg", "md5": "988c2bd44be09e8c4fc1df5514e3e5de_jpg", "height": 480}, {"url": "http://yxfile.idealsee.com/a13b54b71197a2cd26dc59f561c2b847_jpg.jpg?imageView2/0/w/480/h/0", "width": 640, "html": "IMG_20140802_113514.jpg", "md5": "a13b54b71197a2cd26dc59f561c2b847_jpg", "height": 480}]}, "is_saved": true, "md5": "a79fd9cec650473f4d4852ecf76a1f9d_jpg", "type": 7, "resize_lock": -1}, {"opacity": 1.0, "sort": 4, "tip_text": "", "repeat": 1, "icon_class": "icon-text", "color": "FFFFFF", "text": "121211<div><br></div>", "focus": false, "t_font_family": "\u9ed1\u4f53", "tem_widget_change_dis": 0, "height": 44, "visible": true, "readOnly": false, "radius": 0.0, "auto_play": 1, "tem_widget_change": 1, "label": "\u6587\u5b571", "id": "58462c7d9cf82200054f0349", "icon": 1, "rotate_x": 0, "rotate_y": 0, "rotate_z": 0, "scale": 0.0, "single_page": 0, "t_font_size": 12, "custom": 0, "width": 100, "offset_x": -15, "offset_y": -12, "offset_z": 0, "action": {"type": -1}, "is_saved": true, "type": 10, "resize_lock": -1}, {"opacity": 1.0, "sort": 5, "tip_text": "", "repeat": 1, "icon_class": "icon-text", "color": "FFFFFF", "text": "etettetfhfhhhgfhghghfgfdfdfddffddfddsdsds", "focus": false, "t_font_family": "\u9ed1\u4f53", "tem_widget_change_dis": 0, "height": 55, "visible": true, "readOnly": false, "radius": 0.0, "auto_play": 1, "tem_widget_change": 1, "label": "\u6587\u5b571", "id": "58462c7d9cf82200054f034a", "icon": 1, "rotate_x": 0, "rotate_y": 0, "rotate_z": 0, "scale": 0.0, "single_page": 0, "t_font_size": 12, "custom": 0, "width": 243, "offset_x": -25, "offset_y": 29, "offset_z": 0, "action": {"type": -1}, "is_saved": true, "type": 10, "resize_lock": -1}, {"opacity": 1.0, "sort": 6, "tip_text": "", "repeat": 1, "icon_class": "icon-text", "color": "FFFFFF", "text": "&nbsp;fdafds\u7231\u7684\u662f\u6cd5\u5e08\u6253\u53d1a<div><br></div>", "focus": false, "t_font_family": "\u9ed1\u4f53", "tem_widget_change_dis": 0, "height": 66, "visible": true, "readOnly": false, "radius": 0.0, "auto_play": 1, "tem_widget_change": 1, "label": "\u6587\u5b571", "id": "58462c7d9cf82200054f034c", "icon": 1, "rotate_x": 0, "rotate_y": 0, "rotate_z": 0, "scale": 0.0, "single_page": 0, "t_font_size": 12, "custom": 0, "width": 122, "offset_x": -126, "offset_y": 247, "offset_z": 0, "action": {"type": -1}, "is_saved": true, "type": 10, "resize_lock": -1}, {"opacity": 1.0, "sort": 7, "tip_text": "", "repeat": 1, "icon_class": "icon-text", "color": "FFFFFF", "text": "dsd<div>sds</div><div>sds</div><div>sds</div><div>sd</div><div>sds</div><div>sds</div><div>sds</div><div><br></div>", "focus": false, "t_font_family": "\u9ed1\u4f53", "tem_widget_change_dis": 0, "height": 198, "visible": true, "readOnly": false, "radius": 0.0, "auto_play": 1, "tem_widget_change": 1, "label": "\u6587\u5b571", "id": "58462c7d9cf82200054f034d", "icon": 1, "rotate_x": 0, "rotate_y": 0, "rotate_z": 0, "scale": 0.0, "single_page": 0, "t_font_size": 12, "custom": 0, "width": 100, "offset_x": 85, "offset_y": -57, "offset_z": 0, "action": {"type": -1}, "is_saved": true, "type": 10, "resize_lock": -1}, {"opacity": 1.0, "sort": 8, "tip_text": "\u62d3\u5c55\u4ecb\u7ecd", "repeat": 1, "icon_class": "icon-image-text", "color": "0EA090", "focus": false, "tem_widget_change_dis": 0, "height": 45, "visible": true, "shape": "circle", "radius": 4.0, "auto_play": 1, "tem_widget_change": 1, "label": "\u62d3\u5c55\u4ecb\u7ecd1", "id": "58462c7d9cf82200054f034b", "icon": 1, "rotate_x": 0, "rotate_y": 0, "rotate_z": 0, "scale": 0.0, "single_page": 0, "readOnly": false, "custom": 0, "width": 135, "offset_x": -2, "offset_y": 347, "offset_z": 624, "action": {"content": "<div style=\"text-align: center;\"><span style=\"color: rgb(51, 51, 51);\">xcvfghff</span><img src=\"http://119.97.153.53/media01/5a44c7ba5bbe4ec867233d67e4806848_jpg-480.jpg\" style=\"text-align: left;color: rgb(51, 51, 51);\"/></div><p style=\"text-align: center;\">gvhhghg</p>", "type": 3}, "is_saved": true, "type": 3, "resize_lock": -1}];
//
// LS.set("widgets", obj);
// LS.setById("widgets", "58462c7d9cf82200054f0343",{id:1000, name:999});
// LS.add("widgets", {id:10001, name:9991});
// var ss = LS.obj();
// var kk = LS.get("widgets");
// LS.each(function(i,j){
//   debugger;
// })

let LS = {
    set: function(key, obj) {
        localStorage.setItem(key, JSON.stringify(obj));
        this.length = LS.length;
        localStorage.setItem("timestamp", new Date().getTime());
    },
    setById: function(key, id, newObj) {
        var obj = this.get(key);
        setItem(obj, id, newObj);
        this.set(key, obj);
    },
    get: function(key) {
        var v = JSON.parse(localStorage.getItem(key))
        return v === null ? undefined : v;
    },
    getById: function(key, id) {
        var  obj = this.get(key);
        var v = getItem(obj, id);
        return v === null ? undefined : v;
    },
    remove: function(key) {
        localStorage.removeItem(key);
        this.length = LS.length;
    },
    add: function(key,newobj){
      var  obj = this.get(key);
      addItem(obj, newobj);
      this.remove(key);
      this.set(key, obj);
    },
    removeById: function(key, id){
      var  obj = this.get(key);
      setItem(obj, id);
      this.remove(key);
      this.set(key, obj);
    },
    clear: function() {
        localStorage.clear();
        this.length = 0;
    },
    each: function(callback) {
        var list = this.obj(),
            key,
            fn = callback || function() {};
        for (key in list) {
            if (fn.call(this, key, localStorage.getItem(key)) === false) {
                break;
            }
        }
    },
    obj: function() {
        var list = {},
            i = 0,
            n, key;
        if (localStorage.isVirtualObject) {
            list = localStorage.key(-1);
        } else {
            n = localStorage.length;
            for (; i < n; i++) {
                key = localStorage.key(i);
                list[key] = localStorage.getItem(key);
            }
        }
        return list;
    },
    length: localStorage.length
};
export default LS;
